<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
class Dispatcher {
    
    /**
     * @return   void
     */
    public function dispatch() {
        $webroot = preg_replace('#/[^/]+$#', '', $_SERVER['SCRIPT_NAME']);
        $router = Router::getInstance();
        $params = $router->parse(isset($_GET['app_param'])?$_GET['app_param']:'');

        unset($_GET['[app_param']);
        if(!empty($_GET)){
            $params = array_merge($params, $_GET);
        }
        $request = Request::getInstance();
        $request->setParams($params);
        $request->setParams($webroot, 'webroot');
        
        $module_class = $params['module'].'Action';
        if (!class_exists($module_class)) {
            Request::getInstance()->render404();
        }
        $module = new $module_class;
        
        $module->setParams($params);
        $module->setView(new PhpView());
        $module->getView()->webroot = $webroot;
        
        $module->execute();
    }
}
